﻿Imports DataSet_ohlTableAdapters
Imports DataSet_ohl
Imports System.Data

Partial Class Empadres_MatrizEmp
    Inherits System.Web.UI.Page
    Private dataSet As DataSet

    Dim TA_Empadre, TA_tempE As New empadreTableAdapter
    Dim DT_Empadre, DT_tempE As New empadreDataTable

    Dim TA_EmpMov As New Emp_MovsTableAdapter
    Dim DT_EmpMov As New Emp_MovsDataTable

    Dim TA_Ubica As New catalogo_generalTableAdapter
    Dim DT_Ubica As New catalogo_generalDataTable

    Dim TA_Animal As New animalTableAdapter
    Dim DT_Animal As New animalDataTable

    Dim boton As New Button
    Dim miBoton As New Button
    Dim botones As New List(Of RadioButton)
    Dim radiob As New RadioButton()

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'boton.Text = "GENERADO"
        'AddHandler boton.Click, AddressOf Me.boton_Click
        'pnlPrueba.Controls.Add(boton)

        'miBoton.Text = "BOTON"
        'AddHandler miBoton.Click, AddressOf Me.miBoton_Click
        'Me.Controls.Add(miBoton)
        ''pnlPrueba.Controls.Add(miBoton)



        'Imports Tom0014TableAdapters
        'Imports Tom0014

        'Dim TA_Ubica As New DataSet_ohlTableAdapters.catalogo_generalTableAdapter
        'Dim DT_Ubica As New DataSet_ohl.catalogo_generalDataTable

        'Dim TA_Animal As New Tom0014TableAdapters.animalTableAdapter
        'Dim DT_Animal As New Tom0014.animalDataTable

        If Not IsPostBack Then
            Dim I As Decimal
            For I = Year(Now) To 2006 Step -1
                ddlAno.Items.Add(I)
            Next
            ddlAno.SelectedIndex = 0
            ddlAno_SelectedIndexChanged(ddlAno, e)
        End If

    End Sub

    Protected Sub imgbtnAno_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgbtnAno.Click
        ddlAno.Items.Insert(0, ddlAno.Items(0).Value + 1)
        ddlAno.SelectedIndex = 0
        ddlAno_SelectedIndexChanged(ddlAno, e)
    End Sub

    Protected Sub rbtPrueba_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtPrueba.SelectedIndexChanged
        txtSalida.Text = "Total :" & rbtPrueba.Items.Count.ToString & "  Indice :" & rbtPrueba.SelectedIndex.ToString & "  Valor :" & rbtPrueba.SelectedValue
        If rbtPrueba.Text = "1" Then
            'txtSalida.Text = "Seleccion : " & rbtPrueba.SelectedValue
        ElseIf rbtPrueba.Text = "2" Then
            'txtSalida.Text = "Seleccion : " & rbtPrueba.SelectedItem.ToString
        End If
    End Sub

    Protected Sub radiob_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim btn As Button = DirectCast(sender, Button)

        btn.BackColor = Drawing.Color.Beige
        btn.ForeColor = Drawing.Color.Maroon
    End Sub

    Private Sub radiob_Click(ByVal sender As Object, ByVal e As EventArgs)
        ' Obtenemos el botón pulsado
        Dim btn As Button = DirectCast(sender, Button)

        ' Lo deshabilitamos
        'btn.Enabled = False
        btn.BackColor = Drawing.Color.Beige
        btn.ForeColor = Drawing.Color.Maroon
        'lblPrueba.Text = "ENTRO"

        ' Mostramos el formulario
        'Dim frm As New FormNuevaFactura
        'frm.ShowDialog()

        ' Habilitamos de nuevo el botón
        'btn.Enabled = True
    End Sub

    Protected Sub btnConfirma_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConfirma.Click
        'ddlAno_SelectedIndexChanged(ddlAno, e)
        DT_Empadre = TA_Empadre.GetEmpadreByAnoN(ddlAno.SelectedValue)
        If DT_Empadre.Rows.Count > 0 Then       'Existen datos en Empadre ?
            Dim xNumCol As Integer = DT_Empadre.Rows.Count
            'Dim xNumCol As Integer = pnlPrueba.Controls.Count
            'Dim xNumCol As Integer = tbPrincipal.Controls.Count
            Dim i, j As Integer
            For j = 0 To 0      'xNumCol - 1
                'txtSalida.Text = botones.Count.ToString
                'txtSalida.Text = pnlPrueba.Controls.Item(j).ID
                'txtSalida.Text = pnlPrueba.Controls.Item(j).Controls.Count
                'txtSalida.Text = tbPrincipal.Controls.Item(j).ID
                'txtSalida.Text = tbPrincipal.Rows.Count
                'txtSalida.Text = tbPrincipal.Rows.Item(j).ID
                'txtSalida.Text = 
                'If botones(j).Checked Then
                'If CType(pnlPrueba.Controls("autoriza0"), RadioButton).Checked Then
                'txtSalida.Text = "Checado"
                'End If
                'If Me.autoriza0.che Then



                Dim xNumEmp As Integer = DT_Empadre(0).empadreId
                Dim actualizado As Integer
                DT_tempE = TA_tempE.GetEmpadreById(xNumEmp)
                If DT_tempE.Rows.Count > 0 Then
                    DT_tempE(0).estatus = "A"
                    'DT_tempE(0).toroId = CType(lblToroId.Text, Integer)
                    actualizado = TA_tempE.Update(DT_tempE)
                    txtSalida.Text = actualizado.ToString + " EMPADRE ACTUALIZADO"
                End If



                Dim TA_t As New movtosTableAdapter
                Dim DT_t As New movtosDataTable
                Dim Row_Movtos As movtosRow
                Row_Movtos = DT_t.NewmovtosRow()

                Row_Movtos.animal = DT_tempE(0).toro
                Row_Movtos.animalid = DT_tempE(0).toroId
                Row_Movtos.f_captura = Today()
                Try
                    Row_Movtos.f_movto = DT_tempE(0).fechaini
                Catch ex As Exception
                    Row_Movtos.f_movto = Today()
                End Try
                Row_Movtos.gan_cor = DT_tempE(0).gan_cor
                Row_Movtos.ubicacion = DT_tempE(0).ubicacion
                Row_Movtos.empadre = DT_tempE(0).empadreId
                Row_Movtos.usuario = "ohernandez"
                Row_Movtos.estatus = "E"
                DT_t.AddmovtosRow(Row_Movtos)
                actualizado = TA_t.Update(Row_Movtos)
                txtSalida.Text = actualizado.ToString + " TORO AGREGADO"

                If Not IsDBNull(DT_tempE(0).fechafin) Then
                    Row_Movtos = DT_t.NewmovtosRow()
                    Row_Movtos.animal = DT_tempE(0).toro
                    Row_Movtos.animalid = DT_tempE(0).toroId
                    Row_Movtos.f_captura = Today()
                    Try
                        Row_Movtos.f_movto = DateAdd(DateInterval.Day, 1, DT_tempE(0).fechafin)
                    Catch ex As Exception
                        Row_Movtos.f_movto = DT_tempE(0).fechafin
                    End Try
                    Row_Movtos.gan_cor = DT_tempE(0).gan_cor
                    Row_Movtos.ubicacion = ""
                    Row_Movtos.empadre = DT_tempE(0).empadreId
                    Row_Movtos.usuario = "ohernandez"
                    Row_Movtos.estatus = "E"
                    DT_t.AddmovtosRow(Row_Movtos)
                    actualizado = TA_t.Update(Row_Movtos)
                    txtSalida.Text = actualizado.ToString + " TORO AGREGADO ff"
                End If


                DT_EmpMov = TA_EmpMov.Get_EM_ByEmp(xNumEmp)
                If DT_EmpMov.Rows.Count > 0 Then
                    Dim xNumEmpMov As Integer = DT_EmpMov.Rows.Count
                    For i = 0 To xNumEmpMov - 1
                        Row_Movtos = DT_t.NewmovtosRow()

                        Row_Movtos.animal = DT_EmpMov(i).vaca
                        Row_Movtos.animalid = DT_EmpMov(i).vacaId
                        Row_Movtos.f_captura = Today()
                        Try
                            Row_Movtos.f_movto = DT_EmpMov(i).fechaini
                        Catch ex As Exception
                            Row_Movtos.f_movto = Today()
                        End Try
                        Row_Movtos.gan_cor = DT_tempE(0).gan_cor
                        Row_Movtos.ubicacion = DT_tempE(0).ubicacion
                        Row_Movtos.empadre = DT_EmpMov(i).empadreId
                        Row_Movtos.usuario = "ohernandez"
                        Row_Movtos.estatus = "E"
                        DT_t.AddmovtosRow(Row_Movtos)
                        actualizado = TA_t.Update(Row_Movtos)
                        txtSalida.Text = actualizado.ToString + " VACA AGREGADA"

                        If Not IsDBNull(DT_tempE(0).fechafin) Then
                            Row_Movtos = DT_t.NewmovtosRow()

                            Row_Movtos.animal = DT_EmpMov(i).vaca
                            Row_Movtos.animalid = DT_EmpMov(i).vacaId
                            Row_Movtos.f_captura = Today()
                            Try
                                Row_Movtos.f_movto = DateAdd(DateInterval.Day, 1, DT_tempE(0).fechafin)
                            Catch ex As Exception
                                Row_Movtos.f_movto = DT_tempE(0).fechafin
                            End Try
                            Row_Movtos.gan_cor = DT_tempE(0).gan_cor
                            Row_Movtos.ubicacion = ""
                            Row_Movtos.empadre = DT_EmpMov(i).empadreId
                            Row_Movtos.usuario = "ohernandez"
                            Row_Movtos.estatus = "E"
                            DT_t.AddmovtosRow(Row_Movtos)
                            actualizado = TA_t.Update(Row_Movtos)
                            txtSalida.Text = actualizado.ToString + " VACA AGREGADA ff"
                        End If

                        DT_Animal = TA_Animal.GetNombreByID(DT_EmpMov(i).vacaId)
                        If DT_Animal.Rows.Count > 0 Then
                            DT_Animal(0).ubicacion = DT_tempE(0).ubicacion
                            TA_Animal.Update(DT_Animal)
                            'actualizado = TA_Animal.Update(DT_Animal)
                            'txtSalida.Text = actualizado.ToString + " VACA ACTUALIZADA"
                        End If
                    Next
                End If



                DT_Animal = TA_Animal.GetNombreByID(DT_tempE(0).toroId)
                If DT_Animal.Rows.Count > 0 Then
                    DT_Animal(0).ubicacion = DT_tempE(0).ubicacion
                    TA_Animal.Update(DT_Animal)
                    'actualizado = TA_Animal.Update(DT_Animal)
                    'txtSalida.Text = actualizado.ToString + " TORO ACTUALIZADO"
                End If

                ddlAno_SelectedIndexChanged(ddlAno, e)
                btnConfirma.Focus()
            Next
        End If
    End Sub

    Protected Sub ddlAno_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlAno.SelectedIndexChanged
        lblTitulo.Text = "MATRIZ   DE   EMPADRES   " & ddlAno.SelectedValue.ToString
        If ddlAno.SelectedIndex = 0 Then
            tbPrincipal = Nothing
        Else
            DT_Empadre = TA_Empadre.GetEmpadreByAnoN(ddlAno.SelectedValue)
            If DT_Empadre.Rows.Count > 0 Then       'Existen datos en Empadre ?
                Dim i, j As Integer
                Dim xNumCol As Integer = DT_Empadre.Rows.Count

                For i = 0 To 3                      'Recorre renglones
                    Dim tblRenglon As New TableRow()
                    tblRenglon.VerticalAlign = VerticalAlign.Top
                    tblRenglon.BorderStyle = BorderStyle.Inset

                    For j = -1 To xNumCol - 1       'Recorre columnas
                        Dim tblCelda As New TableCell()
                        Dim lbl As New Label
                        lbl.ID = "celda" & i.ToString & j.ToString

                        If i = 0 Then               'Renglon Ubicacion
                            If j = -1 Then          'Columna de encabezados (Ubicacion)
                                lbl.Text = "UBICACION"
                                lbl.ForeColor = Drawing.Color.Blue    'LightYellow LemonChiffon Wheat
                                'lbl.BackColor = Drawing.Color.LightYellow 'PaleGreen YellowGreen
                                lbl.Font.Bold = True
                                tblCelda.Controls.Add(lbl)              'PowderBlue SkyBlue - Blue
                            Else
                                Try                 'Checa si ubicacion es Nulo
                                    lbl.Text = DT_Empadre(j).ubicacion
                                Catch ex As Exception
                                    lbl.Text = ""
                                End Try

                                'Dim objeto As Control
                                'objeto = New RadioButton
                                'objeto.ID = "autoriza" & j.ToString

                                'Dim botonList As New List(Of RadioButton)
                                'botones.Add(objeto)

                                'botonList.Add(Of Object)()
                                'Me.pnlPrueba.Controls.Add(objeto)
                                'tblCelda.Controls.Add(objeto)
                                ''Me.pnlPrueba.Controls.Add(botones)

                                Dim chkAll As New CheckBox
                                chkAll.ID = "AUTORIZA" & LTrim(j.ToString)
                                chkAll.Text = "AUTORIZA" & LTrim(j.ToString)
                                chkAll.CssClass = "Botones"
                                tblCelda.Controls.Add(chkAll)

                                Dim radiob As New RadioButton()
                                radiob.ID = "AUTORIZA" & LTrim(j.ToString)
                                radiob.Text = "AUTORIZA" & LTrim(j.ToString)
                                'radiob.Text = "AUTORIZA " & (j + 1).ToString
                                radiob.CssClass = "Botones"
                                radiob.GroupName = "Grid"
                                'pnlPrueba.Controls.Add(radiob)
                                '''''tblCelda.Controls.Add(radiob)

                                ''Dim boton As New Button
                                'boton.ID = "Autoriza" & j.ToString
                                'boton.Text = "AUTORIZA" & j.ToString
                                'boton.CssClass = "Botones"
                                'AddHandler boton.Click, AddressOf Me.boton_Click
                                'pnlPrueba.Controls.Add(boton)
                                ''miBoton.OnClientClick("prueba(lbl.Text)")
                                'tblCelda.Controls.Add(boton)

                                DT_Ubica = TA_Ubica.GetCatGralByTipCve("U", lbl.Text)
                                If DT_Ubica.Rows.Count > 0 Then lbl.Text = DT_Ubica(0).catnom
                                tblCelda.Controls.Add(lbl)
                            End If

                        ElseIf i = 1 Then           'Renglon Clave Semental
                            If j = -1 Then          'Columna de encabezados (Clave Semental)
                                lbl.Text = "SEMENTAL"
                                lbl.ForeColor = Drawing.Color.Blue
                                lbl.Font.Bold = True
                                tblCelda.Controls.Add(lbl)
                            Else
                                lbl.Text = DT_Empadre(j).toro
                                lbl.ForeColor = Drawing.Color.Maroon
                                lbl.BackColor = Drawing.Color.PaleGreen
                                lbl.Font.Bold = True
                                tblCelda.Controls.Add(lbl)
                            End If

                        ElseIf i = 2 Then           'Renglon Nombre Semental
                            If j = -1 Then          'Columna de encabezados (Nombre Semental)
                                lbl.Text = "NOMBRE"
                                lbl.ForeColor = Drawing.Color.Blue
                                lbl.Font.Bold = True
                                tblCelda.Controls.Add(lbl)
                            Else
                                lbl.Text = DT_Empadre(j).nombre
                                tblCelda.Controls.Add(lbl)
                            End If

                        ElseIf i = 3 Then           'Renglon Hembras (GRID)
                            If j = -1 Then          'Columna de encabezados (Hembras)
                                lbl.Text = "HEMBRAS"
                                lbl.ForeColor = Drawing.Color.Blue
                                lbl.Font.Bold = True
                                tblCelda.Controls.Add(lbl)
                            Else
                                DT_EmpMov = TA_EmpMov.Get_EM_ByEmp(CType(DT_Empadre(j).empadreId, Integer))
                                If DT_EmpMov.Rows.Count > 0 Then        'Existen datos en EmpMovs ?

                                    Dim grdEmp As New GridView
                                    grdEmp.AutoGenerateColumns = False

                                    Dim xbound1, xbound2, xbound3 As New BoundField
                                    xbound1.HeaderText = "VACAS"
                                    xbound2.HeaderText = "INICIO"
                                    xbound3.HeaderText = "FIN"
                                    xbound1.DataField = "Vaca"
                                    xbound2.DataField = "fechaini"
                                    xbound3.DataField = "fechafin"
                                    xbound2.DataFormatString = "{0:d}"
                                    xbound3.DataFormatString = "{0:d}"
                                    xbound2.ItemStyle.Width = 90
                                    xbound3.ItemStyle.Width = 90
                                    grdEmp.Columns.Add(xbound1)
                                    grdEmp.Columns.Add(xbound2)
                                    grdEmp.Columns.Add(xbound3)

                                    grdEmp.ID = "grdVE" & i.ToString & j.ToString
                                    grdEmp.DataSource = DT_EmpMov
                                    grdEmp.DataBind()
                                    'grdEmp.Columns.Item(0).Visible = False
                                    'grdEmp.Columns.Item(1).HeaderText = "grdVE" & i.ToString & j.ToString

                                    tblCelda.Controls.Add(grdEmp)
                                End If
                            End If
                        Else

                        End If
                        tblCelda.Controls.Add(New LiteralControl())
                        tblRenglon.Cells.Add(tblCelda)
                    Next
                    tbPrincipal.Rows.Add(tblRenglon)

                Next
            End If
        End If
    End Sub

    Private Function prueba(ByRef param As String) As String
        Dim miString As String
        miString = param
        Return miString
    End Function

    'Public Sub New(ByVal btn As Button)
    '_btn = btn
    'End Sub

    'Public Sub New()

    'End Sub

    Protected Sub btnConf2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConf2.Click
        'For Each row In gvCorral.Rows
        'Dim chk As CheckBox = row.FindControl("chkItemSelect" + sIndice)

        'If chk IsNot Nothing AndAlso chk.Checked Then 'Si está seleccionado obten el id del Animal
        'Dim IdAnimal As Integer = Convert.ToInt32(gvCorral.DataKeys(row.RowIndex).Value)
        'iRegistrosActualizados = iRegistrosActualizados + AnimalAdapter.Update(sNuevaUbicacion, IdAnimal)
        'bSelecciono = True
        'End If
        'Next

        'ddlAno_SelectedIndexChanged(ddlAno, e)
        For Each row In tbPrincipal.Rows
            txtSalida.Text = ""
            'Dim rbPrueba As New RadioButton = ro
            For Each Cell In row.Cells
                Dim rbPrueba As RadioButton = row.FindControl("AUTORIZA1")
                If rbPrueba IsNot Nothing AndAlso rbPrueba.Checked = True Then
                    txtSalida.Text = rbPrueba.ID & "-True"
                Else
                    txtSalida.Text = rbPrueba.ID & "-False"
                End If
                'Response.Write(Cell.ToString)
                'txtSalida.Text = Cell.ToString
            Next
        Next

    End Sub
End Class
